package com.newshub.admin.controller;

import com.newshub.common.Result;
import com.newshub.business.dto.PageResult;
import com.newshub.admin.entity.RssFetchLog;
import com.newshub.admin.service.RssFetchLogService;
import com.newshub.admin.service.RssFeedService;
import com.newshub.logging.annotation.BusinessLog;
import com.newshub.logging.annotation.LogLevel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 后台管理 - 抓取日志控制器
 */
@RestController
@RequestMapping("/api/admin/fetch-logs")
@CrossOrigin(origins = "*")
public class AdminFetchLogController {
    
    @Autowired
    private RssFetchLogService rssFetchLogService;
    
    @Autowired
    private RssFeedService rssFeedService;
    
    /**
     * 分页查询抓取日志
     */
    @GetMapping("/page")
    @BusinessLog(module = "抓取日志管理", description = "分页查询抓取日志", level = LogLevel.INFO)
    public Result<PageResult<RssFetchLog>> getPage(
            @RequestParam(value = "rssFeedId", required = false) Long rssFeedId,
            @RequestParam(value = "status", required = false) String status,
            @RequestParam(value = "startDate", required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @RequestParam(value = "endDate", required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate,
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "size", defaultValue = "10") int size,
            @RequestParam(value = "sortField", defaultValue = "fetchTime") String sortField,
            @RequestParam(value = "sortOrder", defaultValue = "desc") String sortOrder) {
        
        PageResult<RssFetchLog> result = rssFetchLogService.getPage(rssFeedId, status, startDate, endDate, page, size, sortField, sortOrder);
        return Result.success(result);
    }
    
    /**
     * 根据RSS源ID查询抓取日志
     */
    @GetMapping("/feed/{rssFeedId}")
    @BusinessLog(module = "抓取日志管理", description = "根据RSS源ID查询抓取日志", level = LogLevel.INFO)
    public Result<PageResult<RssFetchLog>> getByFeedId(
            @PathVariable Long rssFeedId,
            @RequestParam(value = "startDate", required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @RequestParam(value = "endDate", required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate,
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "size", defaultValue = "10") int size) {
        
        PageResult<RssFetchLog> result = rssFetchLogService.getByFeedId(rssFeedId, startDate, endDate, page, size);
        return Result.success(result);
    }
    
    /**
     * 根据ID查询单条记录
     */
    @GetMapping("/{id}")
    @BusinessLog(module = "抓取日志管理", description = "查询抓取日志详情", level = LogLevel.INFO)
    public Result<RssFetchLog> getById(@PathVariable Long id) {
        RssFetchLog log = rssFetchLogService.getById(id);
        if (log == null) {
            return Result.error("记录不存在");
        }
        return Result.success(log);
    }
    
    /**
     * 删除单条记录
     */
    @DeleteMapping("/{id}")
    @BusinessLog(module = "抓取日志管理", description = "删除抓取日志记录", level = LogLevel.WARN)
    public Result<Void> deleteById(@PathVariable Long id) {
        rssFetchLogService.deleteById(id);
        return Result.success();
    }
    
    /**
     * 批量删除记录
     */
    @DeleteMapping("/batch")
    @BusinessLog(module = "抓取日志管理", description = "批量删除抓取日志记录", level = LogLevel.WARN)
    public Result<Void> deleteByIds(@RequestBody List<Long> ids) {
        rssFetchLogService.deleteByIds(ids);
        return Result.success();
    }
    
    /**
     * 获取抓取统计信息
     */
    @GetMapping("/statistics")
    @BusinessLog(module = "抓取日志管理", description = "获取抓取统计信息", level = LogLevel.INFO)
    public Result<Object> getStatistics(
            @RequestParam(value = "rssFeedId", required = false) Long rssFeedId,
            @RequestParam(value = "startDate", required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @RequestParam(value = "endDate", required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        
        Object statistics = rssFetchLogService.getStatistics(rssFeedId, startDate, endDate);
        return Result.success(statistics);
    }
    
    /**
     * 批量获取RSS源名称映射
     */
    @GetMapping("/rss-feed-names")
    @BusinessLog(module = "抓取日志管理", description = "批量获取RSS源名称映射", level = LogLevel.INFO)
    public Result<Map<Long, String>> getRssFeedNames(@RequestParam("ids") List<Long> ids) {
        Map<Long, String> nameMap = rssFeedService.getNamesByIds(ids);
        return Result.success(nameMap);
    }
}
